<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="110px"
    >
      <el-form-item label="完工/变更编号" prop="designCompletionNumber">
        <el-input
          v-model="queryParams.designCompletionNumber"
          placeholder="请输入设计完工单编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="任务单编号" prop="taskNumber">
        <el-input
          v-model="queryParams.taskNumber"
          placeholder="请输入任务单编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
       <el-form-item label="任务类型" prop="dictTaskType">
        <el-select
          v-model="queryParams.dictTaskType"
          placeholder="请选择任务类型"
          clearable
          class="!w-220px"
        >
          <el-option
            v-for="dict in getIntDictOptions('task_type')"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="客户代码" prop="customerCode">
        <el-input
          v-model="queryParams.customerCode"
          placeholder="请输入客户代码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="产品编号" prop="productNumber">
        <el-input
          v-model="queryParams.productNumber"
          placeholder="请输入产品编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="部件类型" prop="partType">
        <el-input
          v-model="queryParams.partType"
          placeholder="请输入部件类型"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      
      <el-form-item label="产品规格" prop="productStandard">
        <el-input
          v-model="queryParams.productStandard"
          placeholder="请输入产品规格"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" type="primary">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  
  <table-width-set ref="tableTitleSet" @refresh="getList" />
  <!-- 列表 -->
  <ContentWrap>
    <div style="width: 100%; margin-bottom: 10px; display: flex; justify-content: space-between">
      <div>
        <el-button
          type="primary"
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['technology:design-complete-ticket:export']"
        >
          数据导出
        </el-button>
      </div>
      <el-button type="primary" @click="openTableTitleSet"> 表头设置 </el-button>
    </div>
    <Table
      :columns="tableTitleList"
      :selection="tableObject.tableList.length ? true : false"
      @register="register"
      :data="tableObject.tableList"
      :loading="tableObject.loading"
      :pagination="{
        total: tableObject.total
      }"
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.currentPage"
      :tableHeight="tableHeight"
    >
      <template #action="{ row }">
        <el-button
          link
          type="primary"
          @click="openForm(row.id, 'update', 'release')"
          v-if="row.dictTicketStatus === 30"
          v-hasPermi="['technology:release-task-ticket:update']"
        >
          编辑
        </el-button>
        <el-button
          link
          type="primary"
          @click="openForm(row.id, 'view', 'release')"
          v-hasPermi="['technology:release-task-ticket:view']"
        >
          查看
        </el-button>
        <el-button
          link
          type="danger"
          @click="handleDelete(row.id)"
          v-hasPermi="['technology:release-task-ticket:delete']"
        >
          删除
        </el-button>
      </template>
    </Table>
  </ContentWrap>
  <!-- 导出 -->
  <ExportFile ref="exportRef" @download="downloadFun" />
  <!-- 表头设置 -->
  <TableWidthSet ref="tableTitleSet" @refresh="getLabelAll" />
</template>

<script setup lang="ts" name="ReleaseTaskTicket">
import { getIntDictOptions } from '@/utils/dict'
import type { CrudSchema } from '@/hooks/web/useCrudSchemas'
import download from '@/utils/download'
import * as DesignCompleteTicketApi from '@/api/technology/designCompleteTicket'
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

let tableTitleList = reactive<CrudSchema[]>([]) // 表头的数据
const tableHeight = ref(0)
const queryParams = reactive({
  pageNo: 1,
  designCompletionNumber: null,
  taskNumber: null,
  dictTaskType:null,
  productNumber: null,
  productName: null,
  partType: null,
  customerCode: null,
  productStandard: null
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

// tableObject：表格的属性对象，可获得分页大小、条数等属性
// tableMethods：表格的操作对象，可进行获得分页、删除记录等操作getUserLabelPage
// 详细可见：https://doc.iocoder.cn/vue3/crud-schema/
const { tableObject, tableMethods } = useTable({
  getListApi: DesignCompleteTicketApi.getDesignCompleteTicketReleasePage, // 分页接口
  defaultParams: queryParams
})

/**表头数据 */
const getLabelAll = async () => {
  try {
    const data = await DesignCompleteTicketApi.getDesignCompleteTicketReleasePage(queryParams)
    const { allSchemas } = useCrudSchemas(data.labels)
    allSchemas.tableColumns.push({
      field: 'action',
      label: '操作',
      align: 'left',
      headerAlign: 'left',
      width: 150
    })
    tableTitleList = allSchemas.tableColumns
    /**表头修改需要刷新表格 */
    getList()
  } finally {
  }
}

// 获得表格的各种操作
const { getList } = tableMethods

/**表头内部方法 */
const parentRef = ref()
// 获取table组件
const register = (parent: any) => {
  parentRef.value = parent
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
   queryFormRef.value.resetFields()
  tableObject.params = queryParams
  getList()
}

/** 添加/修改操作 */
const { push } = useRouter() // 路由
const openForm = (id: number, type: string, moduleName: string) => {
  console.log(id)
  push({
    path:'/technology/technology/drawingProduction/releaseTaskTicketDetail',
    query: {
      id,
      type,
      moduleName
    }
  })
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await DesignCompleteTicketApi.deleteDesignCompleteTicket(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const exportRef = ref()
const handleExport = () => {
  exportRef.value.open()
}
// 导出
const downloadFun = async () => {
  const val = exportRef.value.params
  let params = {
    fileName: val.name,
    ids: ''
  }
  if (val.range === 'all') {
    params.ids = ''
  } else {
    params.ids = parentRef.value.selections.map((item) => item.id).join(',')
  }
  console.log(params)
  DesignCompleteTicketApi.exportDesignCompleteTicket(params).then((res) => {
    download.excel(res, val.name)
    parentRef.value.clearTableSelect()
  })
}
/**打开表格表头设置**/
const tableTitleSet = ref()
const openTableTitleSet = () => {
  let code = 'designCompleteTicket'
  console.log(tableTitleList)
  tableTitleSet.value.open(tableTitleList, code)
}

/** 初始化 **/
onMounted(async () => {
  const queryParamsHeight = queryFormRef.value.$el.clientHeight + 2
  tableHeight.value = document.body.clientHeight - 132 - queryParamsHeight - 115
  await getLabelAll()
  getList()
})
</script>
